package com.jianyun.jyzs.utils;

import android.Manifest;
import android.annotation.TargetApi;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.provider.MediaStore;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v4.content.FileProvider;
import android.util.Log;
import android.widget.Toast;

import com.google.gson.Gson;
import com.jianyun.jyzs.activity.CanvasActivity;
import com.jianyun.jyzs.activity.CreateEngineerActivity;
import com.jianyun.jyzs.activity.VideoImageGridActivity;
import com.jianyun.jyzs.bean.EngineerAttach;
import com.jianyun.jyzs.bean.EngineeringResult;
import com.jianyun.jyzs.bean.User;
import com.jianyun.jyzs.constant.SysConstant;
import com.jianyun.jyzs.dao.EngineerDao;
import com.jianyun.jyzs.http.Api;
import com.jianyun.jyzs.http.CustomCallback;
import com.jianyun.jyzs.http.RetrofitHelper;
import com.jianyun.jyzs.widget.LoadingDialog;


import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.jianyun.jyzs.utils.RceLog;
import cn.rongcloud.rce.lib.model.StaffInfo;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by mlli on 2017/11/2.
 * 创建工程动态的帮助类
 */

public class EngineerCreateHelper {
    private int cameraPermissionReqCode = 201;
    private int videoPermissionReqCode = 204;
    public static final int REQUEST_CODE_CAMERA = 11;
    public static final int REQUEST_CODE_SELECT_VIDEO = 14;
    private CreateEngineerActivity context;

    public EngineerCreateHelper(CreateEngineerActivity activity) {
        this.context = activity;
    }

    @TargetApi(23)
    public void openVideoWithPermission() {
        if (!CommonUtils.isExitsSdcard()) {
            Toast.makeText(context, "视频需要sdcard支持！", Toast.LENGTH_SHORT).show();
            return;
        }
        if (ContextCompat.checkSelfPermission(context, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
            //有权限
            Intent intent = new Intent(context, VideoImageGridActivity.class);
            context.startActivityForResult(intent, REQUEST_CODE_SELECT_VIDEO);
        } else {
            ActivityCompat.requestPermissions(context, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.CAMERA}, videoPermissionReqCode);
        }
    }

    //打开照相机
    @TargetApi(23)
    public void openCameraWithPermission() {
        if (ContextCompat.checkSelfPermission(context, Manifest.permission.CAMERA)
                == PackageManager.PERMISSION_GRANTED) {
            //有权限
            if (!CommonUtils.isExitsSdcard()) {
                Toast.makeText(context.getApplicationContext(), "拍照需要sdcard支持！", Toast.LENGTH_SHORT).show();
                return;
            }
            context.cameraFile = new File(PathUtil.imagePathName, System.currentTimeMillis() + ".jpg");
            context.cameraFile.getParentFile().mkdirs();

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                Uri uriForFile = FileProvider.getUriForFile(context, "com.jianyun.jyzs.FileProvider", context.cameraFile);

                context.startActivityForResult(new Intent(MediaStore.ACTION_IMAGE_CAPTURE).putExtra(MediaStore.EXTRA_OUTPUT, uriForFile), REQUEST_CODE_CAMERA);

            } else {
                context.startActivityForResult(new Intent(MediaStore.ACTION_IMAGE_CAPTURE).putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(context.cameraFile)), REQUEST_CODE_CAMERA);

            }
           // context.startActivityForResult(new Intent(MediaStore.ACTION_IMAGE_CAPTURE).putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(context.cameraFile)), REQUEST_CODE_CAMERA);
        } else {
            ActivityCompat.requestPermissions(context,
                    new String[]{Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE},
                    cameraPermissionReqCode);
        }
    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        RceLog.d("lml", "requestCode=" + requestCode + "resultCode=" + resultCode);
        if (requestCode == 90 & data != null) {
            Uri selectedImage = data.getData(); //获取系统返回的照片的Uri
            String[] filePathColumn = {MediaStore.Images.Media.DATA};
            Cursor cursor = context.getContentResolver().query(selectedImage,
                    filePathColumn, null, null, null);//从系统表中查询指定Uri对应的照片
            cursor.moveToFirst();
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String path = cursor.getString(columnIndex);  //获取照片路径
            String imgPath="file://" + path;
            cursor.close();
            Log.i("test","data:"+path);
            ArrayList<EngineerAttach> list = new ArrayList<>();
            String extName = FileUtil.getFileExtName(imgPath.toString());
            EngineerAttach attach = new EngineerAttach(1, imgPath, path, extName);
            list.add(attach);

            context.attachExpandView.setAttach(list);
        } else if (requestCode == 100 & data != null) {
            String path = data.getStringExtra("path");
            RceLog.d("lml", path.toString());
            ArrayList<EngineerAttach> list = new ArrayList<>();
            String extName = FileUtil.getFileExtName(path.toString());
            EngineerAttach attach = new EngineerAttach(2, "file://" + path, path, extName);
            list.add(attach);
            context.attachExpandView.setAttach(list);
        } else if (requestCode == REQUEST_CODE_CAMERA) {
            RceLog.d("lml", "REQUEST_CODE_CAMERA==95");
            if (context.cameraFile != null && context.cameraFile.length() > 0) {
                Intent intent = new Intent(context, CanvasActivity.class);
                intent.putExtra("imgPath", ImageUtils.getScaledImage(context.cameraFile.getAbsolutePath()));
                context.startActivityForResult(intent, 150);
            }
        } else if (requestCode == 150 & data != null) {
            final String imgPath = data.getStringExtra("filePath");
            RceLog.d("lml", "____" + imgPath);
            ArrayList<EngineerAttach> list = new ArrayList<>();
            String extName = FileUtil.getFileExtName(imgPath.toString());
            EngineerAttach attach = new EngineerAttach(1, "file://" + imgPath, imgPath, extName);
            list.add(attach);
            context.attachExpandView.setAttach(list);
        } else if (requestCode == REQUEST_CODE_SELECT_VIDEO & data != null) {
            String filePath = data.getStringExtra("path");
            RceLog.d("lml", "video" + filePath);
            String extName = FileUtil.getFileExtName(filePath.toString());
            EngineerAttach attach = new EngineerAttach(3, "file://" + filePath, filePath, extName);
            ArrayList<EngineerAttach> list = new ArrayList<>();
            list.add(attach);
            context.attachExpandView.setAttach(list);
        } else if (requestCode == 110 & data != null) {
            String place = data.getStringExtra(SysConstant.msg_key_string1);
            String id = data.getStringExtra(SysConstant.msg_key_string2);
            context.tvProject.setText(place);
            context.projectId = id;
            context.tvPlace.setText(null);
            context.tvPlace.setText(null);
        } else if (requestCode == 130 & data != null) {
            String type = data.getStringExtra(SysConstant.msg_key_string1);
            context.tvType.setText(type);
        } else if (requestCode == 120 & data != null) {
            String loc = data.getStringExtra(SysConstant.msg_key_string1);
            context.tvPlace.setText(loc);

        } else if (requestCode == 140 & data != null) {
            RceLog.d("lml","140");
            ArrayList<User> list = (ArrayList<User>) data.getSerializableExtra(Const.SELECTED_CONTACTS);
            if (list==null||list.size()==0){
                context.tvLook.setText("全部可见");
                context.staffInfoArrayList.clear();
            }else {
                context.staffInfoArrayList.addAll(list);
                context.tvLook.setText("部分可见");
            }
        }
    }



}
